<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>demo PDF展示</title>

</head>
<body>
<div id="pdf-wrap" style="width: 100%">
    <div>
        <!--<img id="image" style="width: 100%" src="">-->
        <button id="prev">Previous</button>
        <button id="next">Next</button>
        &nbsp; &nbsp;
        <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span>
    </div>
    <canvas id="pdf-canvas"></canvas>
</div>

<script src="static/js/jquery.min.js"></script>
<script src="static/js/lib/pdf/pdf.js"></script>
<script src="static/js/app/consts/consts.js"></script>
<script src="static/js/app/util/common.js"></script>
<script>
    let pdfDisplayWidth = document.getElementById("pdf-wrap").offsetWidth;
    if (!pdfDisplayWidth) {
        // pdf展示宽度默认值px
        pdfDisplayWidth = 300;
    }
    let pdfDoc = null,
        pageNum = 1,
        pageRendering = false,
        pageNumPending = null,
        canvas = document.getElementById('pdf-canvas'),
        ctx = canvas.getContext('2d');
    // $.ajax({
    //     type: "GET",
    //     url: baseUrl + "/api/wechat/personal/pdf",
    //     success: function (msg) {
    //         let file = msg.data;
    //         $("#image").prop("src", "data:image/jpg;base64," + file);
    //     }
    // });

    var url = '//cdn.mozilla.net/pdfjs/tracemonkey.pdf';
    // Base64字符串方式
    // let pdfData = atob("base64String");
    // let loadingTask = PDFJS.getDocument({data: pdfData});
    let loadingTask = PDFJS.getDocument(url);
    loadingTask.then(function(pdfDoc_) {
        pdfDoc = pdfDoc_;
        document.getElementById('page_count').textContent = pdfDoc.numPages;
        // Initial/first page rendering
        renderPage(pageNum);
    });

    /**
     * Get page info from document, resize canvas accordingly, and render page.
     * @param num Page number.
     */
    function renderPage(num) {
        pageRendering = true;
        // Using promise to fetch the page
        pdfDoc.getPage(num).then(function(page) {
            var viewport = page.getViewport(1);
            var scale = pdfDisplayWidth / viewport.width;
            var viewport = page.getViewport(scale);
            canvas.height = viewport.height;
            canvas.width = viewport.width;

            // Render PDF page into canvas context
            var renderContext = {
                canvasContext: ctx,
                viewport: viewport
            };
            var renderTask = page.render(renderContext);

            // Wait for rendering to finish
            renderTask.promise.then(function() {
                pageRendering = false;
                if (pageNumPending !== null) {
                    // New page rendering is pending
                    renderPage(pageNumPending);
                    pageNumPending = null;
                }
            });
        });

        // Update page counters
        document.getElementById('page_num').textContent = num;
    }

    /**
     * If another page rendering in progress, waits until the rendering is
     * finised. Otherwise, executes rendering immediately.
     */
    function queueRenderPage(num) {
        if (pageRendering) {
            pageNumPending = num;
        } else {
            renderPage(num);
        }
    }

    /**
     * Displays previous page.
     */
    function onPrevPage() {
        if (pageNum <= 1) {
            return;
        }
        pageNum--;
        queueRenderPage(pageNum);
    }
    document.getElementById('prev').addEventListener('click', onPrevPage);

    /**
     * Displays next page.
     */
    function onNextPage() {
        if (pageNum >= pdfDoc.numPages) {
            return;
        }
        pageNum++;
        queueRenderPage(pageNum);
    }
    document.getElementById('next').addEventListener('click', onNextPage);
</script>
</body>
</html>